package com.gongpingjialoan.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.ValueReportActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.AssessmentData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.network.NetDataJson.OnNetDataJsonListener;
import com.gongpingjialoan.utility.Utils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CarStatusDialog extends Dialog
{
    
    private CarStatusDialog mySelf = this;
    
    private NetDataJson mNetDataJson;
    
    Context context;
    
    private Handler messageHanlder;
    
    private Button yes_layout;
    
    private String status;
    
    private TextView status_txt;
    
    private TextView tips_txt;
    
    private TextView message_txt;
    
    static CarStatusDialog carStatusDialog;
    
    public CarStatusDialog(Context context, String status, Handler messageHanlder)
    {
        super(context, R.style.dialogs);
        this.context = context;
        this.messageHanlder = messageHanlder;
        this.status = status;
    }
    
    public CarStatusDialog(Context context, String status)
    {
        super(context, R.style.dialogs);
        this.context = context;
        this.status = status;
    }
    
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.car_status_dialog);
        this.setCanceledOnTouchOutside(false);
        yes_layout = (Button)this.findViewById(R.id.submit_btn);
        tips_txt = (TextView)this.findViewById(R.id.tips_txt);
        status_txt = (TextView)this.findViewById(R.id.status_txt);
        message_txt = (TextView)this.findViewById(R.id.message_txt);
        String str = "";
        List<String> lst = new ArrayList<String>();
        if (status.equals("excellent"))
        {
            status_txt.setText("优秀");
            tips_txt.setText(Utils.getCarStatusCountData().get(0));
            
            lst = Utils.getCarStatusData().get(0);
            for (int i = 1; i <= lst.size(); i++)
            {
                str += (i + "、" + lst.get(i - 1) + "\n");
            }
        }
        else if (status.equals("good"))
        {
            status_txt.setText("较好");
            tips_txt.setText(Utils.getCarStatusCountData().get(1));
            
            lst = Utils.getCarStatusData().get(1);
            for (int i = 1; i <= lst.size(); i++)
            {
                str += (i + "、" + lst.get(i - 1) + "\n");
            }
        }
        else if (status.equals("fair"))
        {
            status_txt.setText("一般");
            tips_txt.setText(Utils.getCarStatusCountData().get(2));
            
            lst = Utils.getCarStatusData().get(2);
            for (int i = 1; i <= lst.size(); i++)
            {
                str += (i + "、" + lst.get(i - 1) + "\n");
            }
        }
        else
        {
            status_txt.setText("较差");
            tips_txt.setText(Utils.getCarStatusCountData().get(3));
            
            lst = Utils.getCarStatusData().get(3);
            for (int i = 1; i <= lst.size(); i++)
            {
                str += (i + "、" + lst.get(i - 1) + "\n");
            }
        }
        message_txt.setText(str);
        
        yes_layout.setOnClickListener(new android.view.View.OnClickListener()
        {
            public void onClick(View v)
            {
                if (messageHanlder != null)
                {
                    Message msg = new Message();
                    msg.what = 1;
                    messageHanlder.sendMessage(msg);
                }
                else
                {
                    AssessmentData ad = AssessmentData.getInstance();
                    // TODO:跳转到H5 页面
                    // String url = "http://m.gongpingjia.com/m/mobile/"
                    String url =
                        "http://m.gongpingjia.com/m/mobile/" + ad.getBrandSlug() + "/" + ad.getModelSlug() + "/"
                            + ad.getModelDetailSlug() + "/" + ad.getYear() + "-" + ad.getMonth() + "/" + ad.getMile()
                            + "/";
                    Utils.LogD("web url = " + url);
                    Intent it = new Intent();
                    it.putExtra("url", url);
                    it.putExtra("title", "估值报告");
                    it.putExtra("content", "");
                    it.setClass(context, ValueReportActivity.class);
                    context.startActivity(it);
                    
                    mNetDataJson = new NetDataJson(new OnNetDataJsonListener()
                    {
                        @Override
                        public void onDataJsonUpdate(JSONObject json)
                        {
                            Utils.LogD("提交估值历史成功");
                            System.out.println("333");
                        }
                        
                        @Override
                        public void onDataJsonError(String errorMessage)
                        {
                            Utils.LogD("提交估值历史失败，失败原因：" + errorMessage);
                            System.out.println("错误:" + errorMessage);
                        }
                    });
                    mNetDataJson.setUrl(API.assessHistoryAdd);
                    mNetDataJson.addParam("device_code", GPJApplication.getInstance().getDeviceCode());
                    mNetDataJson.addParam("brand", ad.getBrandSlug());
                    mNetDataJson.addParam("model", ad.getModelSlug());
                    mNetDataJson.addParam("model_detail", ad.getModelDetailSlug());
                    mNetDataJson.addParam("year", ad.getYear());
                    mNetDataJson.addParam("month", ad.getMonth());
                    mNetDataJson.addParam("mile", ad.getMile());
                    mNetDataJson.addParam("city", ad.getCity());
                    mNetDataJson.addParam("condition", ad.getStatus());
                    mNetDataJson.addParam("pricetype", "");
                    mNetDataJson.request("post");
                }
                mySelf.dismiss();
            }
        });
         mySelf.setCanceledOnTouchOutside(true);
    }
}
